package com.aayush.libs.location;

import java.util.List;

import android.content.Context;
import android.location.Location;
import android.util.Log;

import com.aayush.libs.generic_structures.JarvisStack;
import com.aayush.libs.location.interfaces.LocationTracker;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;

class TrackingLocationListener extends DefaultUserLocationListener implements LocationTracker{
	private final String TAG = getClass().getSimpleName(); 
	
	private static final long DEFAULT_MIN_RECORD_TIME = 6000;	// 1 minute minimum time to record location
	private static final float DEFAULT_MIN_RECORD_DISTANCE = 10f;	//10 meters is default minimum distance
	
	private long minTime = DEFAULT_MIN_RECORD_TIME;			
	private float minDistance = DEFAULT_MIN_RECORD_DISTANCE;
	
	private JarvisStack<Location> mLocations;				//stack for user location.
	
	
	public TrackingLocationListener(Context context) {
		super(context);
		mLocations = new JarvisStack<Location>();
	}
	
	// This will be called when user location is changed
	@Override
	public void onLocationChanged(Location newLocation) {
		Location lastRecoredLocation = mLocations.top();
		
		long timeDiff = newLocation.getTime() - lastRecoredLocation.getTime();
		float distanceDiff = newLocation.distanceTo(lastRecoredLocation);
		
		Log.d(TAG,"Time Diffterence is: "+timeDiff+" Distance Difference is: "+distanceDiff);
		
		if(timeDiff <= minTime && distanceDiff <= minDistance){
			mLocations.push(newLocation);
		}
		
		super.onLocationChanged(newLocation);
	}

	public long getMinTimeDifference() {
		return minTime;
	}

	public void setMinTimeDifference(long minTimeDifference) {
		this.minTime = minTimeDifference;
	}

	public float getMinDistanceDifference() {
		return minDistance;
	}

	public void setMinDistanceDifference(float minDistanceDifference) {
		this.minDistance = minDistanceDifference;
	}

	public JarvisStack<Location> getmLocations() {
		return mLocations;
	}

	@Override
	public boolean startTracker() {
		
		if(!isGooglePlayServiceOn())
			return false;
		
		mLocations = new JarvisStack<Location>();
		locationManager.requestLocationUpdates(provider, minTime, minDistance, this);
		
		return true;
	}

	@Override
	public boolean resumeTracker() {
		if(!isGooglePlayServiceOn())
			return false;
		
		locationManager.requestLocationUpdates(provider, minTime, minDistance, this);
		return true;
	}

	@Override
	public Location pauseTracker() {
		
		locationManager.removeUpdates(this);
		return locationManager.getLastKnownLocation(provider);
	}

	@Override
	public Location stopTracker() {
		locationManager.removeUpdates(this);
		return null;
	}

	@Override
	public void reset() {
		
	}

	@Override
	public JarvisStack<Location> getAllUserLocationsInOrder() {
		return mLocations;
	}
	@Override
	public List<Location> getAllUserLocationsInList() {
		
		return mLocations.toList();
	}

	boolean isGooglePlayServiceOn()
	{
		int status = GooglePlayServicesUtil
				.isGooglePlayServicesAvailable(context);

		// Showing status
		if (status != ConnectionResult.SUCCESS) { // Google Play Services are
													// not available
			Log.e(LocationTracker.TAG,"No google play service. Status code: "+status+"." +
					" Please get google play service for tracker to work");
			
			return false;
		}
		return true;
	}
}
